﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AMail.Common.Interfaces.Data;

namespace AMail.Modules.Data.Actions
{
  public class DeleteMessagesAction : IMailAction
  {
    private List<IMailMessage> deletedMessages;
    private IMailFolder previousParentFolder;

    public DeleteMessagesAction(IEnumerable<IMailMessage> messages, IMailFolder previousParentFolder)
    {
      this.deletedMessages = messages.ToList();
      this.previousParentFolder = previousParentFolder;
    }

    #region IMailAction Members

    public void Commit(IMailArchiver archiver)
    {
      foreach (var message in deletedMessages)
        archiver.DeleteMessage(message);
    }

    public void Undo()
    {
      foreach (var message in deletedMessages)
        message.ParentFolder = this.previousParentFolder;
    }

    #endregion

    public override string ToString()
    {
      if (this.deletedMessages.Count == 1)
        return string.Format("Deleted message '{0}' from '{1}'", this.deletedMessages[0].Subject, this.previousParentFolder.Name);
      else
        return string.Format("Deleted {0} messages from '{1}'", this.deletedMessages.Count, this.previousParentFolder.Name);
    }

  }
}
